﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

using Enyim.Caching.Configuration;
using Enyim.Caching.Memcached;
using Enyim.Caching;

namespace MemcachedTest
{
    class Program
    {
        static void Main(string[] args)
        {
            MemcachedClientConfiguration config = new MemcachedClientConfiguration();//创建配置参数
            config.Servers.Add(new System.Net.IPEndPoint(IPAddress.Parse("127.0.0.1"), 11211));//增加服务节点
            config.Protocol = MemcachedProtocol.Text; // 1.4以上才支持Binary，低于1.4只能Text
            config.SocketPool.ConnectionTimeout = new TimeSpan(0, 0, 5);
            //config.Protocol = MemcachedProtocol.Text; // 要加验证模式，必须Text
            //config.Authentication.Type = typeof(PlainTextAuthenticator);//设置验证模式
            //config.Authentication.Parameters["userName"] = "memcache";//用户名参数
            //config.Authentication.Parameters["password"] = "password";//密码参数

            var mc = new MemcachedClient(config);//创建连接

            // 测试，由于使用Binary模式连接1.2.6版服务，总是超时/false
            //var st = mc.Stats();
            //bool ret;
            //ret = mc.Store(StoreMode.Add, "key1", 1);//写入
            //ret = mc.Store(StoreMode.Set, "key1", 2);//更新

            mc.Store(StoreMode.Add, "key1", 1);//写入
            mc.Store(StoreMode.Set, "key1", 2);//更新

            // 指定过期时间
            //mac.Store(StoreMode.Set, key, str, new TimeSpan(24, 0, 0));
            //mac.Store(StoreMode.Add, key, str, new TimeSpan(24, 24, 24));

            var val = mc.Get("key1");//获取信息

            Console.WriteLine("key1: {0}", val);

            // 清除缓存（并不释放内存，全标记为过期）
            mc.FlushAll();

            Console.ReadKey();
        }
    }
}
